package com.caiyi.financial.nirvana.sms.gateway.sgip.message.body;

import com.caiyi.financial.nirvana.sms.gateway.cmpp.CMPPMessageTool;
import com.caiyi.financial.nirvana.sms.gateway.sgip.message.SGIPBody;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SGIPReport extends SGIPBody {

    private static final Logger logger = LoggerFactory.getLogger(SGIPReport.class);

    private int sbmtSpNodeNum;    //该命令所涉及的Submit或deliver命令的序列号，由源节点编号、日期时间、编号三部分组成
    private int sbmtTimestamp;
    private int sbmtSequence;
    private int reportType;        //Report命令类型	0：对先前一条Submit命令的状态报告 	1：对先前一条前转Deliver命令的状态报告
    private String userNumber;    //接收短消息的手机号，手机号码前加“86”国别标志
    private int stat;        //该命令所涉及的短消息的当前执行状态 0：发送成功 	1：等待发送 	2：发送失败
    private int erorCode;    //当State=2时为错误码值，否则为0
    private String reserved;

    @Override
    public int encode(ByteBuf buffer) {
        throw new UnsupportedOperationException("not need");
    }

    /**
     * 解码
     */
    @Override
    public SGIPBody decode(ByteBuf buffer) {

        //序列号
        this.sbmtSpNodeNum = buffer.readInt();
        this.sbmtTimestamp = buffer.readInt();
        this.sbmtSequence = buffer.readInt();

        //Report命令类型
        this.reportType = buffer.readByte();

        //21字节接收短消息的手机号
        byte[] temp = new byte[21];
        buffer.readBytes(temp);
        temp = CMPPMessageTool.trimStringBufferRightZeros(temp);
        this.userNumber = new String(temp).intern();

        //执行状态
        this.stat = buffer.readByte();

        //错误码
        this.erorCode = buffer.readByte();

        //保留项
        temp = new byte[8];
        buffer.readBytes(temp);
        this.reserved = new String(temp);

        return this;
    }

    public long getRemoteMsgId() {
        return (long) sbmtTimestamp * 10000 + sbmtSequence;
    }

    public int getSbmtSpNodeNum() {
        return sbmtSpNodeNum;
    }

    public void setSbmtSpNodeNum(int sbmtSpNodeNum) {
        this.sbmtSpNodeNum = sbmtSpNodeNum;
    }

    public int getSbmtTimestamp() {
        return sbmtTimestamp;
    }

    public void setSbmtTimestamp(int sbmtTimestamp) {
        this.sbmtTimestamp = sbmtTimestamp;
    }

    public int getSbmtSequence() {
        return sbmtSequence;
    }

    public void setSbmtSequence(int sbmtSequence) {
        this.sbmtSequence = sbmtSequence;
    }

    public int getReportType() {
        return reportType;
    }

    public void setReportType(int reportType) {
        this.reportType = reportType;
    }

    public String getUserNumber() {
        if (this.userNumber.startsWith("86")) {
            this.userNumber = this.userNumber.substring(2);
        }
        return userNumber;
    }

    public void setUserNumber(String userNumber) {
        this.userNumber = userNumber;
    }

    public int getStat() {
        return stat;
    }

    public void setStat(int stat) {
        this.stat = stat;
    }

    public int getErorCode() {
        return erorCode;
    }

    public void setErorCode(int erorCode) {
        this.erorCode = erorCode;
    }

    public String getReserved() {
        return reserved;
    }

    public void setReserved(String reserved) {
        this.reserved = reserved;
    }

    @Override
    public String toString() {
        return "SGIPReport [logger=" + logger + ", sbmtSpNodeNum=" + sbmtSpNodeNum + ", sbmtTimestamp=" + sbmtTimestamp
                + ", sbmtSequence=" + sbmtSequence + ", reportType=" + reportType + ", userNumber=" + userNumber
                + ", stat=" + stat + ", erorCode=" + erorCode + ", reserved=" + reserved + "]";
    }
}
